package com.yundian.user.action;


import java.sql.Timestamp;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yundian.user.service.IUserService;
import com.yundian.user.vo.User;


@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
    String uName;
    String uPassword;
    String uEmail;
	protected User user;
	protected IUserService userService;
	String ActionMsg=null;
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		
		User u = userService.validateUser(user.getUsername(),user.getPassword());
		if(u != null){
			Map session = ActionContext.getContext().getSession();
			session.put("user",u);
			return SUCCESS;
		}
			
		return ERROR;
	}
	
	public String register() throws Exception {
//		 if(uName!=null&&uPassword!=null&&uEmail!=null){
//			User user=new  User("admin", model.getUName(), MD5.MD5Encode(model.getUPassword()), model.getUEmail(),1, 0.0, 0) ;
//			if(service.checkUser(model.getUEmail())){
//				user.setUUpdateTime(new Timestamp(System.currentTimeMillis()));
//			   service.addUser(user);
//			   user=service.loginUser(model.getUEmail(), MD5.MD5Encode(model.getUPassword()));
//			   if(user!=null) { 	
//			      ServletActionContext.getRequest().getSession().setAttribute("LoginUser", user);
//			      return "finish";
//			     }
//			}else{
//				ActionMsg="The Email is exit";
//				return "registerStep1";
//			}
//		   }
//			return "finish"
//		
		
		userService.saveUser(user);
		return SUCCESS;
	}
	
	public String logout() throws Exception {
		Map session = ActionContext.getContext().getSession();
		session.remove("user");
		return SUCCESS;
	}
	
	public IUserService getUserService() {
		return userService;
	}

	public String getUName() {
		return uName;
	}

	public void setUName(String name) {
		uName = name;
	}

	public String getUPassword() {
		return uPassword;
	}

	public void setUPassword(String password) {
		uPassword = password;
	}

	public String getUEmail() {
		return uEmail;
	}

	public void setUEmail(String email) {
		uEmail = email;
	}

	public String getActionMsg() {
		return ActionMsg;
	}

	public void setActionMsg(String actionMsg) {
		ActionMsg = actionMsg;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
